package com.thread;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CompletionServiceTest {
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		ExecutorService executor = Executors.newFixedThreadPool(10); // 创建含10.条线程的线程池
		CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(executor);
		for (int i = 1; i <= 10; i++) {
			final int result = i;
			completionService.submit(new Callable<Integer>() {
				public Integer call() throws Exception {
					Thread.sleep(new Random().nextInt(5000)); // 让当前线程随机休眠一段时间
					return result;
				}
			});
		}
		System.out.println(completionService.take().get()); // 获取执行结果
	}
}